"""MCP Client Tool Management Module"""
from typing import List, Dict, Any
from mcp import ClientSession
from mcp.types import CallToolResult


class Tool:
    """Represents a tool with its properties"""

    def __init__(self, server: str, name: str, description: str, input_schema: Dict[str, Any]):
        self.server = server
        self.name = name
        self.description = description
        self.input_schema = input_schema

    def format_for_llm(self) -> str:
        """Format tool information for LLM"""
        args_desc = []
        if "properties" in self.input_schema:
            for param_name, param_info in self.input_schema["properties"].items():
                arg_desc = f"- {param_name}: {param_info.get('description', 'No description')}"
                if param_name in self.input_schema.get("required", []):
                    arg_desc += " (required)"
                args_desc.append(arg_desc)

        return f"""
MCP Server Name: {self.server}
MCP Tool Name: {self.name}
MCP Tool Description: {self.description}
MCP Tool Arguments:
{chr(10).join(args_desc)}
"""


class ToolManager:
    """Manages tool-related operations"""

    def __init__(self, server: str, session: ClientSession):
        self.server = server
        self.session = session

    async def list_tools(self) -> List[Tool]:
        """List available tools from the server"""
        response = await self.session.list_tools()
        return [
            Tool(self.server, tool.name, tool.description, tool.inputSchema)
            for tool in response.tools
        ]

    async def execute_tool(self, tool_name: str, arguments: Dict[str, Any]) -> CallToolResult:
        """Execute a tool with given arguments"""
        return await self.session.call_tool(tool_name, arguments)
